/**
 * 
 */
package id.web.faisalabdillah.xtimesheet.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceException;
import javax.persistence.Query;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.JpaTemplate;
import org.springframework.stereotype.Component;

import com.google.appengine.api.datastore.Key;

import id.web.faisalabdillah.xtimesheet.dao.IRoleDao;
import id.web.faisalabdillah.xtimesheet.domain.Role;
import id.web.faisalabdillah.xtimesheet.domain.User;

/**
 * @author faitz
 *
 */
@Component
public class RoleDaoImpl implements IRoleDao {
	
	private JpaTemplate jpaTemplate;
	
	@Autowired
	private void setJpaTemplate(EntityManagerFactory entityManagerFactory){
		jpaTemplate=new JpaTemplate(entityManagerFactory);
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#insert(id.web.faisalabdillah.xtimesheet.domain.Role)
	 */
	@Override
	public void insert(Role role) {
		jpaTemplate.persist(role);
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#update(id.web.faisalabdillah.xtimesheet.domain.Role)
	 */
	@Override
	public void update(Role role) {
		jpaTemplate.merge(role);

	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#remove(id.web.faisalabdillah.xtimesheet.domain.Role)
	 */
	@Override
	public void remove(Role role) {
		jpaTemplate.remove(role);

	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#findById(com.google.appengine.api.datastore.Key)
	 */
	@Override
	public Role findById(Key key) {
		return jpaTemplate.find(Role.class, key);
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#getByCode(java.lang.String)
	 */
	@Override
	public Role getByCode(String code) {
		return (Role) jpaTemplate.find("select r from Role r where r.roleCode = ?1", new Object[]{"code"}).get(0);
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#sizeAll()
	 */
	@Override
	public int sizeAll() {
		return jpaTemplate.execute(new JpaCallback<Integer>() {

			@Override
			public Integer doInJpa(EntityManager em)
					throws PersistenceException {
				Query q=em.createQuery("select count(1) from Role");
				return (Integer) q.getSingleResult();
			}
			
		});
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#listAll()
	 */
	@Override
	public List<Role> listAll() {
		return jpaTemplate.find("select r from Role r");
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#listAllPaged(int, int)
	 */
	@Override
	public List<Role> listAllPaged(final int first, final int max) {
		return jpaTemplate.execute(new JpaCallback<List<Role>>() {

			@Override
			public List<Role> doInJpa(EntityManager em)
					throws PersistenceException {
				Query q=em.createQuery("Select r from Role");
				q.setFirstResult(first);
				q.setMaxResults(max);
				return q.getResultList();
			}
		});
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#searchByCode(java.lang.String)
	 */
	@Override
	public List<Role> searchByCode(String code) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#searchByCodeSize(java.lang.String)
	 */
	@Override
	public int searchByCodeSize(String code) {
		// TODO Auto-generated method stub
		return 0;
	}

	/* (non-Javadoc)
	 * @see id.web.faisalabdillah.xtimesheet.dao.IRoleDao#searchByNamePaged(java.lang.String, int, int)
	 */
	@Override
	public List<Role> searchByNamePaged(String name, int first, int max) {
		// TODO Auto-generated method stub
		return null;
	}

}
